﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CUREBusinessLogicLayer;
using CUREDataAccessLayers;
using System.Net;
using System.Net.Sockets;

namespace CURE
{
    /// <summary>
    /// 系统登录
    /// </summary>
    public partial class Login : System.Web.UI.Page
    {
        LoginBLL lobll = new LoginBLL();

        /// 初始加载
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = Request.QueryString["action"];
            if (action == "out")
            {
                //退出后重新登录
                string userID = Request.QueryString["userid"];
                string strHostName = Request.QueryString["host"];

                bool lb = lobll.DeleteOnline(userID, strHostName);
            }
        }

        /// 登录
        protected void btn_Login_Click(object sender, EventArgs e)
        {
            lbl_State.Text = string.Empty;
            //获取密码以及判断用户名是否存在
            string str_Note = Convert.ToString(lobll.GetPassword(txt_UserName.Value));

            string strHostName = Dns.GetHostName();
            string userID = lobll.GetID(txt_UserName.Value).ToString().Trim();
            string userName = txt_UserName.Value.Trim();


            //用户名不能为空
            if (txt_UserName.Value == string.Empty)
            {
                lbl_State.Text = "请输入用户名！";
                txt_UserName.Focus();
                return;
            }

            bool lb_login = lobll.IsOnline(userID, strHostName);
            if (lb_login)
            {
                //登录过没注销  免密登录
            }
            else
            {
                //密码不能为空
                if (txt_Password.Value == string.Empty)
                {
                    lbl_State.Text = "请输入密码！";
                    txt_Password.Focus();
                    return;
                }
                //用户名不存在
                if (str_Note == null)
                {
                    lbl_State.Text = "用户名不存在！";
                    return;
                }
                //密码错误
                if (str_Note != MasterDal.GetMD5Hash(txt_Password.Value.Trim()))
                {
                    lbl_State.Text = "输入的密码不正确！";
                    return;
                }
                bool lb = lobll.WriteOnline(userID, userName, strHostName);
                if (!lb)
                {
                    lbl_State.Text = "登录日志生成错误！";
                    return;
                }
            }

            //获取Cookie
            HttpCookie cookie = Request.Cookies["MWS_User"];

            //Cookie记录是否存在
            if (cookie == null)
            {
                cookie = new HttpCookie("MWS_User");
            }
            cookie.Values.Set("NAME", userName);
            cookie.Values.Set("GROUP_ID", lobll.GetGROUP_ID(txt_UserName.Value).ToString().Trim());
            cookie.Values.Set("ID", userID);
            Response.SetCookie(cookie);
            cookie.Expires.AddMinutes(10);

            Response.Redirect("Index.aspx");
        }

        /// 重置
        protected void btn_New_Click(object sender, EventArgs e)
        {
            txt_UserName.Value = string.Empty;
            txt_Password.Value = string.Empty;
            lbl_State.Text = string.Empty;
            txt_UserName.Focus();
        }
    }
}